/*
 * @author Jordi Mateo
 * @author Sergi Builtrago
 * @author Marc Gonzalez
 * @author Aitor Garcia
 * @version 23.12.2012
 */

package com.example.project;

import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;



public class OnMenjar extends MapActivity {
	MapView mapView;
	List<Overlay> mapOverlays;
	MapItemizedOverlay itemizedoverlay;
	private static final String LIST_TAB_TAG = "tab1";
    private static final String MAP_TAB_TAG = "tab2";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activityon_menjar);
		   
		final TabHost host = (TabHost)findViewById(android.R.id.tabhost);
		host.setup();
        TabSpec spec = host.newTabSpec("tab1");
        spec.setContent(R.id.tab1);
        spec.setIndicator("Lista");
         
        host.addTab(spec);
        spec = host.newTabSpec("tab2");
        spec.setContent(R.id.tab2);
        spec.setIndicator("Mapa");
        host.addTab(spec);
        
        mapView =(MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapView.postInvalidate();
        
        mapOverlays = mapView.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
        itemizedoverlay = new MapItemizedOverlay(drawable, this);
        GeoPoint point = new GeoPoint(19240000,-99120000);
	    OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!", "I'm in Mexico City!");
	    GeoPoint point2 = new GeoPoint(35410000, 139460000);
	    OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!", "I'm in Japan!");
	    itemizedoverlay.addOverlay(overlayitem);
	    itemizedoverlay.addOverlay(overlayitem2);
	    mapOverlays.add(itemizedoverlay);
        
        final ListView listView = (ListView) findViewById(R.id.list);
        listView.setEmptyView((TextView) findViewById(R.id.empty));
       
        List<GeoPoint> pointsList = new ArrayList<GeoPoint>();
        pointsList.add(new GeoPoint((int)(19240000), (int)(-99120000)));
        pointsList.add(new GeoPoint((int)(35410000), (int)(139460000)));
        listView.setAdapter(new ArrayAdapter<GeoPoint>(OnMenjar.this, android.R.layout.simple_list_item_1, pointsList));

        listView.setOnItemClickListener(new OnItemClickListener() {
        	    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        	        GeoPoint geoPoint = (GeoPoint) listView.getAdapter().getItem(position);
        	        if(geoPoint != null) {
        	            setMapZoomPoint(geoPoint, 12);
        	            host.setCurrentTab(1);
        	        }
        	       
        	    }
        	    
        	});
       host.setOnTabChangedListener(new OnTabChangeListener(){
        	public void onTabChanged(String tabName) {
		        if(tabName.equals(MAP_TAB_TAG)) {
		        	
		        }
		        else if(tabName.equals(LIST_TAB_TAG)) {
		        	mapView.getController().zoomToSpan(itemizedoverlay.getLatSpanE6(), itemizedoverlay.getLonSpanE6());
		        }
		    }
        });
        
        host.setCurrentTab(1);
        host.setCurrentTab(0);
	}
	private void setMapZoomPoint(GeoPoint geoPoint, int zoomLevel) {
		    mapView.getController().setCenter(geoPoint);
		    mapView.getController().setZoom(zoomLevel);
		    mapView.postInvalidate();
		}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activityon_menjar, menu);
		return true;
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	
}
